package com.zb.jsp.service;

import java.util.List;

import com.zb.jsp.dao.TblAdvertAddressDao;
import com.zb.jsp.pojo.TblAdvertAddress;
import com.zb.jsp.utils.PropertiesCopyUtil;
import com.zb.jsp.vo.AdvertAddressVo;

public class AdvertAddressService {

	TblAdvertAddressDao tblAdvertAddressDao = new TblAdvertAddressDao();

	/**
	 * 将AdvertAddressVo中的信息保存到数据库中
	 * 
	 * @param advertAddressVo
	 *            AdvertAddressVo对象要保存到数据库中的信息
	 */
	public void addAdvertAddress(AdvertAddressVo advertAddressVo) {

		TblAdvertAddress tblAdvertAddress = new TblAdvertAddress();
		// 调用外部自定义方法把Vo的值赋值给POJO
		PropertiesCopyUtil.copy(tblAdvertAddress, advertAddressVo);

		tblAdvertAddressDao.addTblAdvertAddress(tblAdvertAddress);
	}

	/**
	 * 根据页面获取到的advertAddressVoQry查询数据库中的信息并返回
	 * 
	 * @param advertAddressVoQry
	 *            页面获取到的advertAddressVo对象
	 * @return List<AdvertAddressVo> 查询到的结果列表
	 */
	public List<AdvertAddressVo> findAdvertAddressVoListByAdvertAddressVoQry(
			AdvertAddressVo advertAddressVoQry) {
		
		// 取得总的记录数
	    int count = tblAdvertAddressDao.findAdvertAddressVoCountByAdvertAddressVoQry(advertAddressVoQry);
	        
	    // 取得总共有多少页(【总的记录数】/【每一页显示多少条记录】+（【总的记录数】%【每一页显示多少条记录】==0?0:1))
	    int totalPages = count / advertAddressVoQry.getNumPerPage()
	        + (count % advertAddressVoQry.getNumPerPage() == 0 ? 0 : 1);
	    
	    advertAddressVoQry.setTotalPages(totalPages);

	    // 取得当前是第几页
	    if(advertAddressVoQry.getCurPage()==null || advertAddressVoQry.getCurPage()==0){
	      advertAddressVoQry.setCurPage(1);
	    }
		return tblAdvertAddressDao
				.findAdvertAddressVoListByAdvertAddressVoQry(advertAddressVoQry);
	}

	/**
	 * 根据页面获取到的id号删除数据库信息
	 * 
	 * @param ids
	 *            拼接好 的id 号
	 */
	public void delAdvertAddress(String ids) {

		// 判断传入的selectId是否为空
		if ("".equals(ids)||ids == null) {
			return ;
		}
		tblAdvertAddressDao.delAdvertAddress(ids);
	}

	/**	
	 * 根据id查询数据库信息并保存在AdvertAddressVo对象中
	 * @param id 页面传回的id值
	 * @return 返回查询到的vo对象
	 */
	public AdvertAddressVo findTblAdvertAddressById(int id) {
		AdvertAddressVo advertAddressVo = new AdvertAddressVo();

		TblAdvertAddress tblAdvertAddress = tblAdvertAddressDao
				.findTblAdvertAddressById(id);
		// 把POJO的值赋值给VO
		PropertiesCopyUtil.copy(advertAddressVo, tblAdvertAddress);

		return advertAddressVo;
	}
	
	  /**
	   * 更新广告位信息
	   * 
	   * @param advertAddressVo
	   *          广告位信息
	   */
	  public void uptAdvertAddress(AdvertAddressVo advertAddressVo) {
		  tblAdvertAddressDao.uptTblAdvertAddress(advertAddressVo);
	  }
}
